import {Command} from '../constant';
import Taro from '@tarojs/taro';
import {Dispatch} from 'typings';
import {IAllReducerProps} from '../types';
import {getReducerData} from '@/redux/store';
import {extraPathsValue} from '@/redux/util';
import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    //获取奖项
    async prizesItem() {
      try {
        // Taro.showLoading();
        // const {
        //   main: {},
        // } = getData();
        let params = {
          turntableActivityId: 1,
        };
        const res = await api.PrizesController.showPrizeItemApi();
        console.log('获取奖项==res===', res);
        if (res.code == 'K-000000') {
          action.commonChange('main.prizesItemList', res.context.createPrizeVOS);
          //-1是无限次，0是没次数
          action.commonChange('main.frequencyNum', res.context.residueTimes);
          // Taro.showToast({
          //   title:res.message,
          //   icon:'none'
          // })
        }
      } catch (e) {
        console.log('获取奖项==e===', e);
        // Taro.hideLoading();
        Taro.showToast({
          title: e.message || '网络不给力，请稍后再试！',
          icon: 'error',
          duration: 2000,
        });
      } finally {
        // Taro.hideLoading();
      }
    },
    //获取活动规则
    async getTurntableActivity() {
      try {
        // const {
        //   main: {},
        // } = getData();
        Taro.showLoading();
        let turntableActivityId = 1;
        const res = await api.PrizesController.getTurntableActivityApi();
        console.log('获取活动规则==res===', res);
        if (res.code == 'K-000000') {
          action.commonChange('main.activityRegulation', res.context);
        }
      } catch (e) {
        console.log('获取活动规则==e===', e);
      } finally {
        Taro.hideLoading();
      }
    },
    //获取中奖公告
    async getWinningAnnouncement() {
      try {
        // const {
        //   main: {},
        // } = getData();
        // Taro.showLoading();
        let params = {
          turntableActivityId: 1,
        };
        const res = await api.PrizesController.getWinningAnnouncementApi();
        console.log('获取中奖公告==res===', res);
        if (res.code == 'K-000000') {
          action.commonChange('main.winningAnnouncementList', res.context);
        }
      } catch (e) {
        console.log('获取中奖公告==e===', e);
      } finally {
        // Taro.hideLoading();
      }
    },
    //查询客户地址
    async _addressInit() {
      try {
        // const {
        //   main: {},
        // } = getData();
        Taro.showLoading();
        const res = await api.customerDeliveryAddressBaseController.findAddressList();
        console.log('查询客户地址====', res);
        if (res.length <= 0) {
          action.commonChange('main.getPrizesModel', false);
          action.commonChange('main.addAddressModel', true);
          action.commonChange('main.selectAddress', false);
        } else {
          action.commonChange('main.getPrizesModel', false);
          action.commonChange('main.addAddressModel', false);
          action.commonChange('main.selectAddress', true);
          action.commonChange('main.selectAddress', true);
          action.commonChange('main.addressList', res);
        }
      } catch (e) {
        console.log('查询客户地址==e===', e);
      } finally {
        Taro.hideLoading();
      }
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageCRouletteGametMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
